//
//  main.m
//  FileHandleDemo
//
//  Created by zhouxiaobo on 15/10/29.
//  Copyright © 2015年 mapath. All rights reserved.
//

#import <Foundation/Foundation.h>

#define PATH @"/Users/zhouxiaobo/Documents/Temps"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        //读取文件
        NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:[NSString stringWithFormat:@"%@/cominfo1.json",PATH]];
        
        NSData *da = [fh readDataOfLength:8];
        NSString *str = [[NSString alloc]initWithData:da encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str);
        
        NSData *da1 = [fh readDataToEndOfFile];
        NSString *str1 = [[NSString alloc]initWithData:da1 encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str1);
        
        //写文件
        NSFileHandle *fh1 = [NSFileHandle fileHandleForWritingAtPath:[NSString stringWithFormat:@"%@/temp.txt",PATH]];
        
        NSData *da2 = [@"li jun" dataUsingEncoding:NSUTF8StringEncoding];
        [fh1 seekToEndOfFile]; //将句柄指针移到文件末尾
        [fh1 writeData:da2];    //写入文件
    }
    return 0;
}
